package com.xindaoapp.happypet.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.TagInfos;
import com.xindaoapp.happypet.bean.TagInfos.TagInfo;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * 选择标签
 * 
 * @author xindaoapp
 * 
 */
public class SelectTagDialog extends Dialog {
    private ListView vListView;
    private TagInfos mTagInfos;
    /**
     * 以选择的内容
     */
    private List<TagInfo> mTagList = new ArrayList<TagInfo>();
    private IRequest<List<TagInfo>> iRequest;

    public SelectTagDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public SelectTagDialog(Context context, int theme, TagInfos infos,
            List<TagInfo> selectedTagInfos, IRequest<List<TagInfo>> iRequest) {
        super(context, theme);
        mTagInfos = infos;
        this.iRequest = iRequest;
        mTagList = selectedTagInfos;
    }

    public SelectTagDialog(Context context, TagInfos info, IRequest<List<TagInfo>> iRequest) {
        super(context);
        mTagInfos = info;
        this.iRequest = iRequest;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_select_tag);

        Window window = getWindow();
        LayoutParams lp = window.getAttributes();
        Display d = window.getWindowManager().getDefaultDisplay();
        lp.width = (int) (d.getWidth() * 0.8F);
        // lp.height = (int) (d.getWidth() * 0.8F);
        window.setAttributes(lp);

        vListView = (ListView) findViewById(R.id.listview);
        vListView.setAdapter(new TagAdapter(mTagInfos.recommendtagarray));
        findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                iRequest.request(mTagList);
                dismiss();
            }
        });
        findViewById(R.id.refursh).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                HappyPetApplication.get().getMoccaApi().getMyTags(new IRequest<TagInfos>() {

                    @Override
                    public void request(TagInfos t) {
                        if (null != t) {
                            mTagInfos = t;
                            vListView.setAdapter(new TagAdapter(mTagInfos.recommendtagarray));
                        }
                    }
                });
            }
        });
    }

    class TagAdapter extends BaseAdapter {
        List<TagInfo> list;

        public TagAdapter(List<TagInfo> list) {
            super();
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size() % 3 == 0 ? list.size() / 3 : list.size() / 3 + 1;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(getContext(), R.layout.list_item_select_tag, null);
                holder.tag1 = (Button) convertView.findViewById(R.id.tag1);
                holder.tag2 = (Button) convertView.findViewById(R.id.tag2);
                holder.tag3 = (Button) convertView.findViewById(R.id.tag3);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            final ViewHolder temp = holder;

            final TagInfo info = list.get(3 * position);
            holder.tag1.setText(info.tagname);
            if (isAdded(info.tagid)) {
                temp.tag1.setSelected(true);
            } else {
                temp.tag1.setSelected(false);
            }
            holder.tag1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (mTagList.contains(info)) {
                        mTagList.remove(info);
                        // temp.tag1.setSelected(false);
                    } else {
                        if (isTagsFull()) {
                            return;
                        }
                        mTagList.add(info);
                        // temp.tag1.setSelected(true);
                    }
                    notifyDataSetChanged();
                }
            });
            if (3 * position + 1 < list.size()) {
                holder.tag2.setVisibility(View.VISIBLE);
                final TagInfo info2 = list.get(3 * position + 1);
                holder.tag2.setText(info2.tagname);
                if (isAdded(info2.tagid)) {
                    temp.tag2.setSelected(true);
                } else {
                    temp.tag2.setSelected(false);
                }
                holder.tag2.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (mTagList.contains(info2)) {
                            mTagList.remove(info2);
                            // temp.tag2.setSelected(false);
                        } else {
                            if (isTagsFull()) {
                                return;
                            }
                            mTagList.add(info2);
                            // temp.tag2.setSelected(true);
                        }
                        notifyDataSetChanged();
                    }
                });
            } else {
                holder.tag2.setVisibility(View.INVISIBLE);
            }
            if (3 * position + 2 < list.size()) {
                holder.tag3.setVisibility(View.VISIBLE);
                final TagInfo info3 = list.get(3 * position + 2);
                holder.tag3.setText(info3.tagname);
                if (isAdded(info3.tagid)) {
                    temp.tag3.setSelected(true);
                } else {
                    temp.tag3.setSelected(false);
                }
                holder.tag3.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (mTagList.contains(info3)) {
                            mTagList.remove(info3);
                            // temp.tag3.setSelected(false);
                        } else {
                            if (isTagsFull()) {
                                return;
                            }
                            mTagList.add(info3);
                            // temp.tag3.setSelected(true);
                        }
                        notifyDataSetChanged();
                    }
                });
            } else {
                holder.tag3.setVisibility(View.INVISIBLE);
            }
            return convertView;
        }

        private boolean isAdded(String tagid) {
            for (int i = 0; i < mTagList.size(); i++) {
                TagInfo info = mTagList.get(i);
                if (info.tagid.equals(tagid)) {
                    return true;
                }
            }
            return false;
        }

        /**
         * 标签是否添加满了
         * 
         * @return
         */
        private boolean isTagsFull() {
            if (mTagList.size() >= 3) {
                Toast.makeText(getContext(), "最多添加3个标签", Toast.LENGTH_SHORT).show();
                return true;
            }
            return false;
        }

        class ViewHolder {
            Button tag1;
            Button tag2;
            Button tag3;
        }

    }

}
